package com.jinxin.platform.cdperprotypay.support;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jinxin.platform.base.common.pojo.BusinessEvent;
import com.jinxin.platform.base.common.pojo.InvokeCfg;
import com.jinxin.platform.base.common.pojo.JsonResult;
import com.jinxin.platform.base.common.support.BusinessDelegate;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysHouse;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysOrg;
import com.jinxin.platform.cdperprotypay.pojo.bo.SysUser;
import com.jinxin.platform.cdperprotypay.pojo.bo.WaterData;
import com.jinxin.platform.cdperprotypay.pojo.domain.RoomUser;
import com.jinxin.platform.cdperprotypay.pojo.vo.DictEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 其他模块数据获取
 *
 * @author : zhangpeng
 * @date : 2020-06-29 14:04:12
 **/
@Slf4j
@Component
public class ModularSupport {

    @Autowired
    private BusinessDelegate businessDelegate;

    public void event(Map map) {
        String eventType = "cdproperty-pay";
        try {
            BusinessEvent event = new BusinessEvent(eventType, map);
            businessDelegate.publishEvent(event);
        } catch (Exception e) {
            log.info("广播异常：{}", e);
        }

    }

    public void eventCharge(Map map) {
        String eventType = "cdproperty-chargePay";
        try {
            BusinessEvent event = new BusinessEvent(eventType, map);
            businessDelegate.publishEvent(event);
        } catch (Exception e) {
            log.info("广播异常：{}", e);
        }

    }

    /**
     * 获取当前用户对象
     *
     * @return SysUser
     */
    public SysUser getCurrentUser() {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-user");
        cfg.setClassName("UserRpcService");
        cfg.setFunName("getLoginUser");
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            return JSON.parseObject(result.getData().toString(), SysUser.class);
        }
        SysUser sysUser = new SysUser();
        sysUser.setUserName("物业");
        return sysUser;
    }

    /**
     * 获取一级区域
     * (这里代表小区)
     */
    public List<SysOrg> getSysOrg() {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-user");
        cfg.setClassName("UserRpcService");
        cfg.setFunName("getManagedOrg");
        JsonResult result = businessDelegate.invoke(cfg);
        log.info("获取门禁的小区记录：{}", result);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysOrg.class);
        }
//        return new ArrayList<>();
        List<SysOrg> sysOrgs = new ArrayList();
        SysOrg sysOrg = new SysOrg();
        sysOrg.setOrgId("1");
        sysOrg.setOrgName("物业小区1期");
        sysOrg.setOrgLevels("1");
        SysOrg sysOrg1 = new SysOrg();
        sysOrg1.setOrgId("2");
        sysOrg1.setOrgName("物业小区2期");
        sysOrgs.add(sysOrg);
        sysOrgs.add(sysOrg1);
        return sysOrgs;
    }

    /**
     * 根据条件获取小区楼栋单元房间信息
     */
    public List<SysHouse> getAllHouse(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getAllHouse");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysHouse.class);
        }
//        return new ArrayList<>();

        List<SysHouse> houses = new ArrayList<>();
        SysHouse sysHouse = new SysHouse();
        sysHouse.setBuildId("1");
        sysHouse.setBuildName("1栋");
        sysHouse.setRoomId("1");
        sysHouse.setRoomName("1001");
        SysHouse sysHouse1 = new SysHouse();
        sysHouse1.setBuildId("2");
        sysHouse1.setBuildName("2栋");
        sysHouse1.setRoomId("2");
        sysHouse1.setRoomName("1020");
        houses.add(sysHouse);
        houses.add(sysHouse1);
        return houses;

    }

    /**
     * 根据房间id获取相关信息
     */
    public SysHouse getRoomById(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getRoomById");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new SysHouse();
            }
            return JSONObject.parseObject(result.getData().toString(), SysHouse.class);
        }
//        return new SysHouse();
        SysHouse sysHouse = new SysHouse();
        sysHouse.setVillageId("1");
        sysHouse.setVillageName("东华小区");
        sysHouse.setBuildId("1");
        sysHouse.setBuildName("1");
        sysHouse.setUnitId("1");
        sysHouse.setUnitName("1");
        sysHouse.setRoomId("1");
        sysHouse.setRoomName("1001");
        sysHouse.setRoomArea("1");
        sysHouse.setIntrathecal("100");
        sysHouse.setPublicStall("20");
        sysHouse.setFloor("7");
        sysHouse.setStatus("1");
        return sysHouse;
    }

    /**
     * 根据小区id获取楼栋
     */
    public List<SysHouse> getBuild(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getBuildByVId");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysHouse.class);
        }
//        return new ArrayList<>();
        List<SysHouse> houses = new ArrayList<>();
        SysHouse sysHouse = new SysHouse();
        sysHouse.setBuildId("1");
        sysHouse.setBuildName("1栋");
        SysHouse sysHouse1 = new SysHouse();
        sysHouse1.setBuildId("2");
        sysHouse1.setBuildName("2栋");
        houses.add(sysHouse);
        houses.add(sysHouse1);
        return houses;
    }

    /**
     * 根据楼栋id获取单元 房间
     */
    public List<SysHouse> getUnit(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getUnitByBId");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysHouse.class);
        }
//        return new ArrayList<>();

        List<SysHouse> houses = new ArrayList<>();
        SysHouse sysHouse = new SysHouse();
        sysHouse.setUnitId("1");
        sysHouse.setUnitName("1单元");
        SysHouse sysHouse1 = new SysHouse();
        sysHouse1.setUnitId("2");
        sysHouse1.setUnitName("2单元");
        houses.add(sysHouse);
        houses.add(sysHouse1);
        return houses;
    }

    /**
     * 根据单元id获取房间信息
     */
    public List<SysHouse> getRoomByUId(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getRoomByUId");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysHouse.class);
        }
//        return new ArrayList<>();

        List<SysHouse> houses = new ArrayList<>();
        SysHouse sysHouse = new SysHouse();
        sysHouse.setRoomId("1");
        sysHouse.setRoomName("1001");
        SysHouse sysHouse1 = new SysHouse();
        sysHouse1.setRoomId("2");
        sysHouse1.setRoomName("1020");
        houses.add(sysHouse);
        houses.add(sysHouse1);
        return houses;
    }

    /**
     * 根据roomId获取居住人员
     */
    public List<RoomUser> getUserByRId(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getUserByRId");
        cfg.setArgs(map);
        log.info("物业缴费模块根据roomId获取居住人员参数{}：", map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), RoomUser.class);
        }
//        return new ArrayList<>();
        List<RoomUser> roomUsers = new ArrayList<>();
        RoomUser roomUser = new RoomUser();
        roomUser.setUserName("coco");
        roomUser.setUserIdcard("114456789879879212");
        roomUser.setUserPhone("13012345678");
        roomUser.setUserType("11");
        roomUser.setUserId("1");
        roomUsers.add(roomUser);

        return roomUsers;
    }

    /**
     * 根据人员电话获取房间信息
     */
    public List<SysHouse> getRoomByUser() {
        HashMap map = new HashMap();
        map.put("userPhone", getCurrentUser().getUserCellphoneNo());
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getRoomByUser");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return new ArrayList<>();
            }
            return JSONObject.parseArray(result.getData().toString(), SysHouse.class);
        }
//        return new ArrayList<>();


        SysHouse sysHouse = new SysHouse();
        sysHouse.setRoomId("1");
        sysHouse.setRoomName("1001");
        ArrayList<SysHouse> objects = new ArrayList<>();
        objects.add(sysHouse);
        return objects;
    }

    /**
     * 发送短信结果
     *
     * @param map
     * @return
     */
    public String fasong(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("faSongDuanXin");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return "";
            }
            return result.getData().toString();
        }
        return "{\n" +
                "    \"status\": 500,\n" +
                "    \"success\": false,\n" +
                "    \"msg\": \"发送失败\",\n" +
                "    \"data\": {\n" +
                "        \"message\": \"账户余额不足\",\n" +
                "        \"code\": \"isv.AMOUNT_NOT_ENOUGH\",\n" +
                "        \"requestId\": \"DED2C710-2ED0-4B3B-9EC9-6B4ADB251734\",\n" +
                "        \"bizId\": null\n" +
                "    }\n" +
                "}  ";
    }

    /**
     * 获取水表数据
     */
    public List<WaterData> getWaterData(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-house");
        cfg.setClassName("HouseRpcService");
        cfg.setFunName("getWaterData");
        cfg.setArgs(map);
        log.info("提供给水表模块的数据：{}", map);
        JsonResult result = businessDelegate.invoke(cfg);
        log.info("水表模块提供的数据：{}", result);
        ArrayList<WaterData> list = new ArrayList<>();
        WaterData waterData1 = new WaterData();
        WaterData waterData2 = new WaterData();
        waterData1.setConsumption("10.25");
        waterData1.setDataTime("2020-02-10 12:20:11");

//        waterData2.setConsumption("1.25");
//        waterData2.setDataTime("1605207600000");

        if (map.get("roomId").equals("1")) {
            list.add(waterData1);
        } else {
            list.add(waterData2);
        }

        return list;

//        if (result.getSuccess()) {
//            if (result.getData() == null) {
//                return new ArrayList<>();
//            }
//            return JSONObject.parseArray(result.getData().toString(),WaterData.class);
//        }
//        return new ArrayList<>();
    }

    public List<DictEntity> getDict(Map map) {
        InvokeCfg cfg = new InvokeCfg();
        cfg.setModuleKey("property-dictionary");
        cfg.setClassName("propertyRpcService");
        cfg.setFunName("getBusinessModule");
        cfg.setArgs(map);
        JsonResult result = businessDelegate.invoke(cfg);
        log.info("result:" + result);
        if (result.getSuccess()) {
            if (result.getData() == null) {
                return null;
            }
            List<DictEntity> dictVos = JSON.parseArray(JSON.parse(JSON.toJSONString(result.getData())).toString(), DictEntity.class);
            log.info("根据字典类型:{},获取到字典:{}", map, dictVos);
            return dictVos;
        } else {
            DictEntity dictVo = new DictEntity();
            dictVo.setDictKey("1");
            dictVo.setDictValue("一室一厅");
            DictEntity dictVo1 = new DictEntity();
            dictVo1.setDictKey("2");
            dictVo1.setDictValue("一室二厅");
            List<DictEntity> list = new ArrayList<>();
            list.add(dictVo);
            list.add(dictVo1);
            return list;
//           log.info("未获取字典内容");
//           return null;
        }
    }


}
